package com.loyal.mwm.handler

import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.base.kit.GsonUtil
import com.base.kit.ResUtil
import com.loyal.client.impl.RxSubscriberListener
import com.loyal.core.helper.ViewHelper
import com.loyal.mwm.R
import com.loyal.mwm.base.BaseClickHandler
import com.loyal.mwm.databinding.ActivityLoginBinding
import com.loyal.mwm.ui.LoginActivity
import com.loyal.mwm.ui.settings.ServerActivity
import com.loyal.rx3.RxUtil

class LoginHandler(activity: LoginActivity?, binding: ActivityLoginBinding?) :
    BaseClickHandler<ActivityLoginBinding>(activity, binding), RxSubscriberListener<String?> {

    fun onClick(view: View) {
        ViewHelper.hideKeyBoard(view)
        _root_ide_package_.com.loyal.mwm.util.UIHandler.delay2Enable(view)
        when (view.id) {
            R.id.submitView -> login()
            R.id.account_clear -> binding?.account?.setText("")
            R.id.password_clear -> binding?.password?.setText("")
            R.id.serverView -> {
                val intent = Intent(activity, ServerActivity::class.java)
                activity.startActivity(intent)
            }
            R.id.forgetView -> {
                val intent = Intent(activity, _root_ide_package_.com.loyal.mwm.ui.ForgetActivity::class.java)
                activity.startActivity(intent)
            }
            R.id.registerView -> toRegister()
        }
    }

    private fun login() {
        val account = ViewHelper.getInputText(binding?.account)
        val password = ViewHelper.getInputText(binding?.password)
        if (TextUtils.isEmpty(account)) {
            activity.showToast("请输入用户名！")
            return
        }
        if (TextUtils.isEmpty(password)) {
            activity.showToast("请输入密码！")
            return
        }
        val accountBean =
            _root_ide_package_.com.loyal.mwm.beans.ObservableAccountBean(account, password)
        //登录页面只有登录
        val subscriber =
            _root_ide_package_.com.loyal.mwm.libs.rxjava.RxProgressSubscriber<String>(activity)
        subscriber.setDialogMessage("登录中...").showProgressDialog(true).setTag(accountBean)
        subscriber.setSubscribeListener(this)
        RxUtil.rxExecute(subscriber.loginByJson(accountBean.toString()), subscriber)
    }

    private fun toRegister() {
        //intentBuilder.putExtra("resId", R.id.submitView);
        //intentBuilder.putExtra("username", "");
        //activity.startActivityForResultByAct(RegisterActivity.class, IntImpl.reqCodeRegister);
    }

    override fun onResult(what: Int, tag: Any?, result: String?) {
        try {
            val resultBean = GsonUtil.json2Object(
                result,
                _root_ide_package_.com.loyal.mwm.beans.ResultBean::class.java,
                _root_ide_package_.com.loyal.mwm.beans.AccountBean::class.java
            ) as _root_ide_package_.com.loyal.mwm.beans.ResultBean<com.loyal.mwm.beans.AccountBean?>?
            val observableAccountBean = tag as _root_ide_package_.com.loyal.mwm.beans.ObservableAccountBean?
            val code = if (null == resultBean) "" else ResUtil.replaceText(resultBean.code)
            val message =
                if (null == resultBean) "解析失败" else ResUtil.replaceText(resultBean.message)
            val accountBean = resultBean?.data
            if (!TextUtils.equals("1", code)) {
                activity.showDialog(message)
                return
            }
            val locked = if (null == accountBean) "" else ResUtil.replaceText(accountBean.locked)
            if (TextUtils.equals("1", locked)) {
                activity.showToast("当前设备已被别的设备锁定")
                return
            }
            val account = ResUtil.replaceText(accountBean?.username)
            _root_ide_package_.com.loyal.mwm.util.PreferUtil.putLoginBean(activity, observableAccountBean)
            //intentBuilder.putExtra("username", account);
            val intent = Intent(activity, _root_ide_package_.com.loyal.mwm.ui.MainActivity::class.java)
            activity.startActivity(intent)
            //intentBuilder.setAction(StrImpl.actionDownload);
            //ImageService.startAction(activity, intentBuilder);
            activity.finish()
        } catch (e: Exception) {
            onError(what, tag, e)
        }
    }

    override fun onError(what: Int, tag: Any?, e: Throwable) {
        activity.showErrorDialog("登录失败", e)
    }
}